package 队列.数组模拟环形队列;

/*
* 正常的队列用不完的,只不过会满和空而已,那么我们模拟的队列依然要符合这个特点,即环形队列
* 思路分析:
* 容量 capacity
* 头 front
* 尾 rear
* 模拟队列 arr[]
* 队列满的条件? (rear+1)%capacity == front
* 队列空的条件? rear = front
* */

public class Queue {
    private int capacity;
    private int front = 0;
    private int rear = 0;
    private int[] arr = new int[capacity];

    public Queue(int capacity){
        this.capacity = capacity;
    }

    // 判断队列是否为空
    private Boolean IsNull(){
        return rear == front;
    }

    // 判断对列是否已满
    private Boolean IsFull(){
        return (rear+1)%capacity == front;
    }

    // 入队列
    public void addQueue(int data){
        if(IsFull()){
            throw new RuntimeException("队列已满,无法入队列!");
        }
        arr[rear] = data;
        rear = (rear+1)%capacity;
    }

    // 出队列
    public int getQueue(){
        if(IsNull()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        int value = arr[front];
        front = (front+1)%capacity;
        return value;
    }

    // 队列中元素个数
    public int getSize(){
        return (rear-front+capacity)%capacity;
    }

    // 显示队列
    public void ShowQueue(){
        for (int i = front; i < front + getSize(); i++) {
            System.out.printf("arr[%d] = %d",i%capacity,arr[i%capacity]);
        }
    }

}
